﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyPoints;
namespace MyRegression
{
    public class Regression
    {
        public static double regression(Points[] GDP, int year)
        {  //计算x，y的平均值
            double avex = 0, avey = 0;
            double a, b;
            foreach (Points m in GDP)
            {
                avex += m.x;
                avey += m.y;
            }
            avex /= GDP.Length;
            avey /= GDP.Length;
            //计算系数a,b
            double numerator = 0;
            double denpminator = 0;
            foreach (Points m in GDP)
            {
                numerator += (m.x - avex) * (m.y - avey);
                denpminator += (m.x - avex) * (m.x - avex);
            }
            b = numerator / denpminator;
            a = avey - b * avex;
            return a + b * year;
        }

    }
}
